import Vue from 'vue'
import VueRouter from 'vue-router'
import { localGet } from '../utils/index'


Vue.use(VueRouter)


const routes = [
  {
    path: "/",
    redirect: "/login"
  },
  {
    path: '/login',
    component: () => import('../views/login')
  },
  {
    path: '/index',
    component: () => import('../views/index'),
    children: [{
      path: "goodmessage",
      component: () => import("../views/shopmess/ShopMessage")
    }, {
      path: "Add",
      component: () => import("../views/addshopgoods/AddGoods")
    }, {
      path: "about",
      component: () => import("../views/about/About")
    }, {
      path: 'manage',
      component: () => import("../views/manage/ManagerUser")
    },
    {
      path: "community",
      component: () => import("../views/community/communication")
    }
    ]
  }
]

const router = new VueRouter({
  mode: 'hash',
  // base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // to and from are both route objects. must call `next`.
  // console.log(to)  //目标路由
  // console.log(from)  //出发路由
  if (to.path == "/login") {
    if (from.path != "/") {
      //如果通过浏览器的回退或者前进 进入login删除本地token
      window.localStorage.removeItem("token")
    }
    next()
  } else if (localGet("token")) {  //如果存在token则进行跳转
    next()
  } else {   //否则就返回登录页面
    next({ path: "/login" })
  }
})
//路由重复点击报错
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

export default router
